package com.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.entity.MmallAdvert;
import com.mall.entity.MmallProduct;
import com.mall.entity.vo.AdvertQuery;
import com.mall.entity.vo.AdvertVo;
import com.mall.service.MmallAdvertService;
import com.mall.service.MmallProductService;
import com.mall.utils.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ppmall
 * @since 2021-09-19
 */
@CrossOrigin
@RestController
@RequestMapping("/mall/advert")
public class MmallAdvertController {

    @Autowired
    MmallAdvertService advertService;
    @Autowired
    MmallProductService productService;

    @PostMapping("/adverts/{cur}/{limit}")
    public R getAdverts(@PathVariable("cur") long cur,
                       @PathVariable("limit") long limit,
                       @RequestBody AdvertQuery advertQuery){
        Page<MmallAdvert> page = new Page<>(cur, limit);
        QueryWrapper<MmallAdvert> wrapper = new QueryWrapper<>();

        String detail = advertQuery.getDetail();
        String begin = advertQuery.getBegin();
        String end = advertQuery.getEnd();

        if(!StringUtils.isEmpty(detail)) {
            wrapper.like("detail", detail);
        }

        if(!StringUtils.isEmpty(begin)) {
            wrapper.ge("begin_time", begin);
        }

        if(!StringUtils.isEmpty(end)) {
            wrapper.le("end_time", end);
        }

        advertService.page(page, wrapper);

        long total = page.getTotal();
        List<MmallAdvert> records = page.getRecords();
        List<AdvertVo> advertVos = new ArrayList<>();

        for(MmallAdvert advert: records) {
            AdvertVo advertVo = new AdvertVo();
            BeanUtils.copyProperties(advert, advertVo);
            MmallProduct product = productService.getById(advert.getProductId());
            advertVo.setProductName(product.getName());
            advertVos.add(advertVo);
        }
        return R.ok().data("total", total).data("rows", advertVos);
    }

    @GetMapping("/advert")
    public R getAdvertById(Integer id){
        MmallAdvert advert = advertService.getById(id);
        return R.ok().data("advert", advert);
    }

    @PutMapping("/advert")
    public R updateAdvertById(@RequestBody MmallAdvert advert) {
        boolean update = advertService.updateAdvertById(advert);
        return update ? R.ok() : R.error();
    }

    @PostMapping("/advert")
    public R saveAdvert(@RequestBody MmallAdvert advert) {
        boolean save = advertService.saveAdvert(advert);
        return save ? R.ok() : R.error();
    }

    @DeleteMapping("/advert/{id}")
    public R deleteAdvertById(@PathVariable("id") Integer id){
        boolean delete = advertService.deleteAdvertById(id);
        return delete ? R.ok() : R.error();
    }
}

